package com.example.qa.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.qa.commonutils.R;
import com.example.qa.entity.ZhQuestion;
import com.example.qa.entity.ZhUser;
import com.example.qa.entity.vo.QuestionVo;
import com.example.qa.entity.vo.UserVo;
import com.example.qa.service.ZhUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-03
 */
@CrossOrigin
@RestController
@RequestMapping("/qa/user")
public class ZhUserController {
@Autowired
    private ZhUserService service;
    @PostMapping("/pageUserCondition/{current}/{limit}")
    public R queryUserPage(@PathVariable("current") int current, @PathVariable("limit") int limit, @RequestBody(required = false) UserVo userVo) {
        Page<ZhUser> zhUserPage = new Page<>(current, limit);
        QueryWrapper<ZhUser> wrapper = new QueryWrapper<>();
        wrapper.eq("status",2);
        if (userVo == null)
            service.page(zhUserPage, wrapper);
        else {

            if (!StringUtils.isEmpty(userVo.getBegin()))
                wrapper.ge("gmt_create", userVo.getBegin());
            if (!StringUtils.isEmpty(userVo.getEnd()))
                wrapper.le("gmt_create", userVo.getBegin());
            wrapper.orderByDesc("gmt_create");

            service.page(zhUserPage, wrapper);
        }
        Long total = zhUserPage.getTotal();
        List<ZhUser> zhUserPageRecords = zhUserPage.getRecords();
        return R.ok().data("total", total).data("rows", zhUserPageRecords);
    }
    @GetMapping("/check/{userId}")
    public R checkUser(@PathVariable String userId)
    {

        ZhUser user = service.getById(userId);
        user.setStatus((short)1);
        service.updateById(user);
        return R.ok();
    }

}

